package Anagram;

import java.util.*;

/**
 * User: Vitalii Serdiuk
 * Date: 03.12.13 13:44
 */
public class Word {
    private String word;

    public Word() {
    }

    public Word(String word) {
        this.word = word;
    }

    private static boolean isAlpha(String name) {
        char[] chars = name.toCharArray();

        for (char c : chars) {
            if(!Character.isLetter(c)) {
                return false;
            }
        }
        return true;
    }

    public static Collection<Word> readWord(Collection<String> fileLines) {

        Set<Word> words = new HashSet<Word>();
        StringTokenizer stringTokenizer;
        for (String line : fileLines) {
            stringTokenizer = new StringTokenizer(line, "\t\n\r,.;:-?! ");

            while (stringTokenizer.hasMoreElements()) {
                String str = stringTokenizer.nextElement().toString();
                if (isAlpha(str)) {
                    words.add(new Word(str.toLowerCase()));
                }
            }
        }
        return words;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    int length(){
        return word.length();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Word)) return false;

        Word word1 = (Word) o;

        if (word != null ? !word.equals(word1.word) : word1.word != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return word != null ? word.hashCode() : 0;
    }

    @Override
    public String toString() {
        return word;

    }


}
